package com.sgb.kjwl.view.ui.activity.self.company

import android.content.Context
import android.content.Intent
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityManagementDynamicBinding
import com.sgb.kjwl.di.self.DaggerSelfComponent
import com.sgb.kjwl.di.self.SelfModule
import com.swgk.core.util.PickSelectEntity
import com.sgb.kjwl.view.ui.fragment.self.company.ManagementDynamicFragment
import com.sgb.kjwl.viewmodel.self.company.ManagementDynamicViewModel
import com.swgk.core.base.di.AppComponent
import javax.inject.Inject

/**
 * 经营动态
 */
class ManagementDynamicActivity : AppActivity() {

    private lateinit var mBinding : ActivityManagementDynamicBinding

    private val mTypes = ArrayList<PickSelectEntity<Any>>()

   private lateinit var mAdapter : ManagementPageAdapter

    @Inject
    lateinit var mViewModel : ManagementDynamicViewModel
    companion object{
        private val mTitles = arrayListOf<String>("全部", "商品", "货运", "网站", "商标转让", "版权转让",
             "专利转让", "公司转让", "资产转让", "服务", "需求", "招聘", "团队", "关注", "资产")
        fun launch(context: Context){
            val intent = Intent(context, ManagementDynamicActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        super.setupActivityComponent(appComponent)
         DaggerSelfComponent.builder().appComponent(appComponent)
             .selfModule(SelfModule()).build().inject(this)
    }

    override fun initView() {
        super.initView()
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_management_dynamic)
        mBinding.include.titleView.text = "经营动态"
        mBinding.include.ivBack.setOnClickListener {
            finish()
        }

        mViewModel.mTypeDatas.observe(this, {
            if (it == null || it.isEmpty()){
                return@observe
            }
            mTypes.clear()
            mTypes.addAll(it)
            initTab(it)
        })
    }

    override fun init() {
        super.init()
        mViewModel.getManagementDynamicTypes(mTitles)
    }

    private fun initTab(types : List<PickSelectEntity<Any>>) {
        mAdapter = ManagementPageAdapter(supportFragmentManager, types)
        mBinding.viewPage.adapter = mAdapter
        mBinding.viewPage.offscreenPageLimit = types.size
        mBinding.tablayout.setupWithViewPager(mBinding.viewPage)
        mBinding.tablayout.tabMode = TabLayout.MODE_AUTO
        mBinding.viewPage.currentItem = 0

    }

    class ManagementPageAdapter(fm : FragmentManager, titles : List<PickSelectEntity<Any>>) : FragmentPagerAdapter(fm) {
        private var mTitles : List<PickSelectEntity<Any>> ?= null
        init {
            this.mTitles = titles
        }
        override fun getCount(): Int {
            if (mTitles != null){
                return mTitles!!.size
            }
            return 0
        }

        override fun getItem(position: Int): Fragment {
            return ManagementDynamicFragment.newInstance(mTitles!![position].no, mTitles!![position].projectscale)
        }

        override fun getPageTitle(position: Int): CharSequence {
            if (mTitles != null){
                return mTitles!![position].projectscale
            }
            return ""
        }
    }

}